import { observable, action } from 'mobx';
import BaseListStore from '../BaseListStore';
import * as Apis from '../../apis/ReportApi';
import * as Bctt from '../../constants/BaseListConstant';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

class ReportDetailStore {
    params = {};// 路由或外部参数
    @observable initResponsed = false;// 是否已经得到初始化数据的响应
    @observable initSuccessed = false;// 初始化数据是否获取成功
    @observable initFailedMsg = ''; // 初始化处理失败的原因
    @observable loading = false;
    @observable title = '';
    @observable rightMenu = [];
    @observable tableStore = new TableStore();

    // 创建组件后，第一次渲染前
    @action
    doNew=(params = {}) => {
      this.params = params;
    }

    // 已创建组件，第一次渲染后
    @action
    doInit=(params = {}) => {
      Apis.getChanceReport('report', { onlySessionKey: false, ...params }).then((data) => {
        this.doInitCallBack(params, data);
      });
    }

    // 已组件卸载，清理垃圾数据
    @action
    doClear = () => {
      this.params = {};
      this.initResponsed = false;
      this.initSuccessed = false;
      this.initFailedMsg = '';
      this.loading = false;
      this.title = '';
      this.rightMenu = [];
      this.tableStore = {};
    }

    @action
    doInitCallBack=(params, data) => {
      this.initResponsed = true;
      if (data.status == 'success' && data.hasright) {
        const datas = data.datas.content;
        this.initSuccessed = true;
        this.title = datas.titleName;
        this.rightMenu = datas.rightMenu;
        this.tableStore = new TableStore();
        this.tableStore.getDatas(data.datas.content.sessionKey);
      } else {
        this.initSuccessed = false;
        if (data.hasright) {
          this.initFailedMsg = data.msgcode;
        } else {
          this.initFailedMsg = getLabel(2012, '对不起你暂时没有权限！');
        }
      }
    }

    @action
    doSearch=(params = {}) => {
      this.loading = true;
      const requestParams = { ...params, ...this.params };
      for (const item in requestParams) {
        if (requestParams[item] == '') {
          delete requestParams[item];
        }
      }
      Apis.getChanceReport('report', requestParams).then((data) => {
        this.loading = false;
        this.tableStore.getDatas(data.datas.content.sessionKey);
      });
    }

    excelExport() {
      const requestParams = {};
      Apis.getChanceReportExcel('report', requestParams).then((data) => {
        window.location = Bctt.ADDR_EXCEL_EXPORT;
      });
    }
}

export default ReportDetailStore;
